import {
	observable,
	action,
	computed
} from 'mobx';
import {
	Tools,
	Bubble,
} from "weaver-mobile";
import * as api from '../../apis/changePassword';
import {
  checkWeakPass
} from "../../util/PASSUtil.js";
const getLabel = Tools.getLabel;

class PasswordStore {
	passwordComplexity = '';

	@observable old = '';
	@observable new = '';
	@observable confirm = '';
	@observable verificationCode = '';
	@observable placeholders = [getLabel(511058, '请输入旧密码！'), '', getLabel(511132, '请再次输入密码'), getLabel(511133, '请输入验证码')];
	@observable date = new Date();
	@observable saveBtnDisabled = false;

	@action setPassword(type, value) {
		this[type] = value;
	}

	@computed get values() {
		return [this.old, this.new, this.confirm, this.verificationCode];
	}

	@computed get isFill() {
		if (!this.old) {
			Bubble.error(getLabel(511058, '请输入旧密码！'));
			return false;
		}
		if (!this.new) {
			Bubble.error(getLabel(511059, '请输入新密码！'));
			return false;
		}
		if (!this.confirm) {
			Bubble.error(getLabel(511060, '请再次输入新密码！'));
			return false;
		}
		if (!this.verificationCode) {
			Bubble.error(getLabel(511061, '请填写验证码！'));
			return false;
		}

		return true;
	}

	@computed get isEqual() {
		if (this.new === this.confirm) {
			return true;
		} else {
			Bubble.error(getLabel(504376, '密码确认不正确!'));
			return false;
		}
	}

	@computed get isPassminlen() {
		if (this.new.length < this.minpasslen) {
			Bubble.error(`${getLabel('20172', "密码长度不能小于")}${ this.minpasslen}`);
			return false;
		} else {
			return true;
		}
	}

	@computed get isMatchRule() {
		const judge = (regExps) => {
			return regExps.every(regExp => regExp.test(this.new))
		}
		if (this.passwordComplexity == '1') {
			const regExps = [/[a-z]+/, /[A-Z]+/, /\d+/];
			if (!judge(regExps)) {
				Bubble.error(getLabel('512768',"新密码不符合要求，必须包含字母大写、字母小写、数字！请重新输入！"));
				return false;
			}
		}
		if (this.passwordComplexity == '2') {
			const regExps = [/[a-zA-Z_]+/, /\W+/, /\d+/];
			if (!judge(regExps)) {
				Bubble.error(getLabel('512769',"新密码不符合要求，必须包含字母、数字、特殊字符！请重新输入！"));
				return false;
			}
		}
		if (this.passwordComplexity == '3') {
			const regExps = [/[a-zA-Z_]+/, /\d+/];
			if (!judge(regExps)) {
				Bubble.error(getLabel('512767', "新密码不符合要求，必须包含字母、数字！请重新输入！"));
				return false;
			}
		}
		if (this.passwordComplexity == "4") {
             const regExps = [/[a-z]+/, /[A-Z]+/,/\d+/,/\W+/];
             if (!judge(regExps)) {
             	Bubble.error(getLabel(521798, "新密码不符合要求，必须为字母大写、字母小写、数字、特殊字符组合！请重新输入！"));
                return false;
            }
        }

		return true;
	}

	@action getPasswordSetting = params => {
		this.saveBtnDisabled = false;
		
		api.getPasswordSetting().then(data => {
			const {
				otherParams
			} = data, {
				tip
			} = otherParams;

			['minpasslen', 'passwordComplexity'].map(v => {
				this[v] = data[v];
			});

			this.placeholders[1] = tip ? tip : getLabel(511059, '请输入新密码！').replace(/！/,'');

		}, error => {});
	}

	//校验不通过、保存不成功会刷新验证码。
	refreshVerificationCode = () => {
		this.date = new Date();
	}

	verify = () => {
		const rules = ['isFill', 'isEqual', 'isPassminlen', 'isMatchRule'];

		for (let i = 0; i < rules.length; i++) {
			const rule = rules[i];

			if (!this[rule]) {
				this.refreshVerificationCode();
				return false
			}
		}

		return true;
	}

	save = (params) => {
		if (!this.verify()) return;

		this.saveBtnDisabled = true;

		checkWeakPass(this.new,()=>{
			api.save({
				passwordold: this.old,
				passwordnew: this.new,
				validatecode: this.verificationCode,
				optype: params&&params.optype
			}).then(data => {
				const {
					message,
					status
				} = data;

				if (status === '1') {
					Bubble.success(message);

					this.clearFormField();
					this.refreshVerificationCode();

					window.em && window.em.checkJsApi('pushToLogin') && window.em.pushToLogin();
				} else {
					this.refreshVerificationCode();
					Bubble.error(message);
				}

				this.saveBtnDisabled = false;
			}, error => {});
		},()=>{
			this.refreshVerificationCode();
			this.saveBtnDisabled = false;
		})
	}

	@observable isChild = false;

	@action setComponentStatus = (hasTitle) => {
		this.isChild = !hasTitle;
	}

	@action clearFormField = () => {
		['old', 'new', 'confirm', 'verificationCode'].forEach(v => {
			this[v] = '';
		});
	}

}

export const passwordStore = new PasswordStore();